*******************
* Exporting table for Global South COVID paper
* 
* last updated: 2020-06-23
* Magdalena Larreboure, Elliott Collins
*******************

/* Run Once
use "../data/01_PHP_w1_weighted" , clear


drop if ${weight}==.

gen incomedrop    = inc_d_earn 
gen employmentdrop  = inc_lostwork
gen anysupport     = new_transfers
replace anysupport = 0 if anysupport==.
gen accessmarkets = fsec_access
gen missedmeals   = fsec_reduced
gen healthaccess   = .

gen poor=isPoor=="Poor"

keep incomedrop employmentdrop accessmarkets healthaccess missedmeals anysupport poor LSMS_WEIGHT
save "../data/PHL_maintable.dta" , replace
*/

set more off
use "$da/PHL_maintable.dta" , clear
loc thissample philipines
gl weight LSMS_WEIGHT

*loc outcomes paid_feb paid_now paid_diff employmentdrop incomedrop work_more_earn work_less_hours work_less_earn missedmeals fsec_meals01 fsec_portions01 anysupport accessmarkets divest borrow
loc outcomes incomedrop employmentdrop accessmarkets missedmeals anysupport incomeincr

loc heterogeneity poor

********************************************************************************************************
********************************************************************************************************
********************************************************************************************************

local sR *
local sig1 `sR'
local sig2 `sR'`sR'
local sig3 `sR'`sR'`sR'
local formatDisp %10.2f

*Complete sample
	foreach var of varlist `outcomes' {
        qui count
        if "`var'"=="incomedrop" loc Count: di %10.0fc r(N)
	qui sum `var' [aw=$weight] 
	loc mpost=`r(mean)'
	loc mpost_`var'_all1:  di %10.2fc r(mean)
	loc `var'_SD :  di %10.2fc r(sd)
	loc sdpr_`var'_all1 "(``var'_SD')"
	
*Heterogeneity dimensions
	
	foreach het of varlist `heterogeneity' {
	forval i=0/1 {
        qui count if `het'==`i'
        if "`var'"=="incomedrop" loc Count_`het'`i': di %10.0fc r(N)

	qui sum `var' [aw=$weight] if `het'==`i'
	loc mpost=`r(mean)'
	loc mpost_`var'_`het'`i':  di %10.2fc r(mean)
	loc `var'_SD :  di %10.2fc r(sd)
	loc sdpr_`var'_`het'`i' "(``var'_SD')"
	
	}
	}
	}
		
	clear all
	set obs 3
	
        g Group=""
        g N=""
           
		   
	g full_sample=0
	replace full_sample = 1 in 1
	replace Group = "All" if full_sample == 1
        replace N="`Count'" if full_sample == 1

	g below_consumption=0
	replace below_consumption = 1 in 2
	replace Group = "below_consumption" if below_consumption == 1
        replace N="`Count_poor1'" if below_consumption == 1

	g above_consumption=0
	replace above_consumption = 1 in 3
	replace Group = "above_consumption" if above_consumption == 1
        replace N="`Count_poor0'" if above_consumption == 1



	foreach var in `outcomes' "healthaccess" { 

		g `var'   = ""	

			replace `var'   = "`mpost_`var'_poor1'" if below_consumption==1
			replace `var'   = "`mpost_`var'_poor0'" if above_consumption==1
			replace `var'   = "`mpost_`var'_all1'" if full_sample==1
			
	}
	
                        label var Group "Group"
                        label var N "Number of households"
                        label var employmentdrop "Drop in employment"
                        label var incomedrop "Drop in income"
                        label var incomeincr "Increase in income"
                        label var accessmarkets "Reduced access to markets"
                        label var missedmeals "Missed or reduced meals"
                        label var anysupport "Received NGO or Govt Support"
                        label var healthaccess "Delayed or skipped healthcare"

                        order Group incomedrop incomeincr employmentdrop accessmarkets healthaccess missedmeals anysupport N //divest borrow work_more_earn
                        
                            preserve
                            keep if Group=="All"
                            destring(incomedrop incomeincr), g(up down)
                            gen ratio = round(up/down, .01)
                            keep incomedrop incomeincr ratio
                            export excel using "$dtab/Inverse Table.xlsx", sheetmodify cell(B24)
                            restore
 
	drop below_consumption above_consumption full_sample Group incomeincr
                      
 	export excel using "$dtab/Main Table.xlsx", sheetmodify cell(B48)






